package tuanvn.iti.vn;

import java.util.List;

import mrtran.iti.vn.Main;
import mrtran.iti.vn.Main.GameView;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;

public class SpriteLuffy {
	int[] DIRECTION_TO_ANIMATION_MAP = { 3, 1, 2, 0 };
	public static final int BMP_COLUMNS = 6;
	public static final int BMP_ROWS = 4;
	int  monsterWidth, widthLuffyMonster;
	public static int luffyWidth;
	int xLuffyCenter, yLuffyCenter;
	public static float xSpeed = 0;
	public static float ySpeed = 0;
	public static int xLuffy = 0;
	int yLuffy = (int) (Main.SCREENHEIGHT/1.8F);
	public static int currentFrame = 0;
	private GameView gameView;
	private Bitmap bmp;
	private int width;
	private int height;
	private List<SpriteLuffy> spriteLuffy;
	
	public SpriteLuffy(GameView gameView, Bitmap bmp,List<SpriteLuffy> spriteLuffy) {
		this.gameView = gameView;
		this.bmp = bmp;
		this.width = bmp.getWidth() / BMP_COLUMNS;
		this.height = bmp.getHeight() / BMP_ROWS;
		this.spriteLuffy = spriteLuffy;
	}

	private void update() {
		xLuffyCenter = xLuffy + width / 2;
		yLuffyCenter = yLuffy + height / 2;
		luffyWidth = width / 2;

		SpriteMonster1.distance1 = (int) Math.sqrt(Math.pow(
				(xLuffyCenter - SpriteMonster1.xMonsterCenter1), 2)
				+ Math.pow((yLuffyCenter - SpriteMonster1.yMonsterCenter1), 2));
		SpriteMonster1.widthLuffyMonster1 = luffyWidth
				+ SpriteMonster1.monsterWidth1;

		SpriteMonster2.distance2 = (int) Math.sqrt(Math.pow(
				(xLuffyCenter - SpriteMonster2.xMonsterCenter2), 2)
				+ Math.pow((yLuffyCenter - SpriteMonster2.yMonsterCenter2), 2));
		SpriteMonster2.widthLuffyMonster2 = luffyWidth
				+ SpriteMonster2.monsterWidth2;
		
		SpriteMonster3.distance3 = (int) Math.sqrt(Math.pow(
				(xLuffyCenter - SpriteMonster3.xMonsterCenter3), 2)
				+ Math.pow((yLuffyCenter - SpriteMonster3.yMonsterCenter3), 2));
		SpriteMonster3.widthLuffyMonster3 = luffyWidth
				+ SpriteMonster3.monsterWidth3;
		
		SpriteMonster4.distance4 = (int) Math.sqrt(Math.pow(
				(xLuffyCenter - SpriteMonster4.xMonsterCenter4), 2)
				+ Math.pow((yLuffyCenter - SpriteMonster4.yMonsterCenter4), 2));
		SpriteMonster4.widthLuffyMonster4 = luffyWidth
				+ SpriteMonster4.monsterWidth4;
		
		SpriteMonster5.distance5 = (int) Math.sqrt(Math.pow(
				(xLuffyCenter - SpriteMonster5.xMonsterCenter5), 2)
				+ Math.pow((yLuffyCenter - SpriteMonster5.yMonsterCenter5), 2));
		SpriteMonster5.widthLuffyMonster5 = luffyWidth
				+ SpriteMonster5.monsterWidth5;
		
		SpriteKimCuong.distanceKimCuong = (int) Math.sqrt(Math.pow(
				(xLuffyCenter - SpriteKimCuong.xKimCuongCenter), 2)
				+ Math.pow((yLuffyCenter - SpriteKimCuong.yKimCuongCenter), 2));
		SpriteKimCuong.widthLuffyKimCuong = luffyWidth
				+ SpriteKimCuong.KimCuongWidth;
		
		SpriteKimCuong2.distanceKimCuong2 = (int) Math.sqrt(Math.pow(
				(xLuffyCenter - SpriteKimCuong2.xKimCuongCenter2), 2)
				+ Math.pow((yLuffyCenter - SpriteKimCuong2.yKimCuongCenter2), 2));
		SpriteKimCuong2.widthLuffyKimCuong2 = luffyWidth
				+ SpriteKimCuong2.KimCuongWidth2;
		
		SpriteKimCuong3.distanceKimCuong3 = (int) Math.sqrt(Math.pow(
				(xLuffyCenter - SpriteKimCuong3.xKimCuongCenter3), 2)
				+ Math.pow((yLuffyCenter - SpriteKimCuong3.yKimCuongCenter3), 2));
		SpriteKimCuong3.widthLuffyKimCuong3 = luffyWidth
				+ SpriteKimCuong3.KimCuongWidth3;
		
		SpriteItem.distanceItem = (int) Math.sqrt(Math.pow(
				(xLuffyCenter - SpriteItem.xItemCenter), 2)
				+ Math.pow((yLuffyCenter - SpriteItem.yItemCenter), 2));
		SpriteItem.widthLuffyItem = luffyWidth
				+ SpriteItem.ItemWidth;
	}

	public void onDraw(Canvas canvas) {
		update();
		int srcX = currentFrame * width;
		int srcY = getAnimationRow() * height;
	
	Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
	Rect dst = new Rect(xLuffy, yLuffy, xLuffy + width, yLuffy + height);
		canvas.drawBitmap(bmp, src, dst, null);

	}

	private int getAnimationRow() {
		double dirDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
		int direction = (int) Math.round(dirDouble) % BMP_ROWS;
		return DIRECTION_TO_ANIMATION_MAP[direction];
	}

}
